package com.numberone.master.modules.oa.util;

import java.nio.charset.Charset;
import java.util.UUID;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONObject;

public class PostUtil {
	/**
	 * 发送post请求
	 * @param jsonObj
	 * @param url
	 * @return 返回response的entity
	 */
	public static String httpPostWithJson(JSONObject jsonObj,String url){
	    //boolean isSuccess = false;
	    String content = "";
	    HttpPost post = null;
	    try {
	        HttpClient httpClient = new DefaultHttpClient();

	        // 设置超时时间
	        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);
	        httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 2000);
	            
	        post = new HttpPost(url);
	        // 构造消息头
	        post.setHeader("Content-type", "application/json; charset=utf-8");
	        post.setHeader("Connection", "Close");  //发送后关闭连接
	        String sessionId = getSessionId();
	        post.setHeader("SessionId", sessionId);
	        //post.setHeader("appid", appId);
	                    
	        // 构建消息实体
	        StringEntity entity = new StringEntity(jsonObj.toString(), Charset.forName("UTF-8"));
	        entity.setContentEncoding("UTF-8");
	        // 发送Json格式的数据请求
	        entity.setContentType("application/json");
	        post.setEntity(entity);
	        
	        HttpResponse response = httpClient.execute(post);
	        HttpEntity responseEntity = response.getEntity();
	        content = EntityUtils.toString(responseEntity, "utf-8");
	        /*BufferedReader ioin = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
	        
	        StringBuffer sb = new StringBuffer("");
	        String line = "";  
            String NL = System.getProperty("line.separator"); 
            while ((line = ioin.readLine()) != null) {  
                sb.append(line + NL);  
            }  
            ioin.close();  
            content = sb.toString();*/
            return content;
	    } catch (Exception e) {
	        e.printStackTrace();
	        //isSuccess = false;
	    }finally{
	        if(post != null){
	            try {
	                post.releaseConnection();
	                Thread.sleep(500);
	            } catch (InterruptedException e) {
	                e.printStackTrace();
	            }
	        }
	    }
		return content;
	    
	}

	// 构建唯一会话Id
	public static String getSessionId(){
	    UUID uuid = UUID.randomUUID();
	    String str = uuid.toString();
	    return str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
	}
}
